package org.jetlinks.community.mstn.term;

import org.hswebframework.ezorm.core.param.Term;
import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.EmptySqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.PrepareSqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.SqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.term.AbstractTermFragmentBuilder;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class MstnJoinTermBuilder extends AbstractTermFragmentBuilder {

    public static final String joinType = "mstn-join";

    public MstnJoinTermBuilder() {
        super(joinType, "关联查询");
    }

    private void acceptTerm(boolean and,  RDBColumnMetadata rdbColumnMetadata, PrepareSqlFragments fragments, Map<String , Object> terms ,String type) {
        String alias = String.valueOf(terms.get("alias"));
//        String type = String.valueOf(terms.get("type"));
        String value = String.valueOf(terms.get("value"));
        String column = alias + ".\"" + rdbColumnMetadata.getName() + "\"";
        fragments.addSql( column + " " + handleTermType(type) + " ? ").addParameter(value);
    }
    private void acceptTerm(boolean and, PrepareSqlFragments fragments, List<Map<String, String>> targets) {

    }
    @Override
    public SqlFragments createFragments(String s, RDBColumnMetadata rdbColumnMetadata, Term term) {
        List<Object> values = this.convertList(rdbColumnMetadata, term);
        if (values.isEmpty()) {
            return EmptySqlFragments.INSTANCE;
        }
        PrepareSqlFragments fragments = PrepareSqlFragments.of();
        List<String> options = term.getOptions();

        Object value = term.getValue();
        boolean and = options.contains("and");

        if (value instanceof Map) {
            acceptTerm(and, rdbColumnMetadata, fragments, (Map<String, Object>) value, options.get(0));
        } else if (value instanceof List) {
//            acceptTerm(and, fragments, (List<Map<String, String>>) value);
        } else {
            String terms = String.valueOf(value);
            //json
            if (terms.startsWith("[")) {
                System.out.println("暂未实现");
            } else if (terms.startsWith("{")) {
                System.out.println("暂未实现");
            } else if (terms.contains(":") && !terms.contains(" ")) {
                System.out.println("暂未实现");
            } else {
                System.out.println("暂未实现");
            }
        }
        return fragments;
    }

    //处理term类型
    private String handleTermType(String termType) {
        switch (termType) {
            case "eq" :
                return "=";
            case "not" :
                return "!=";
            case "like" :
                return "like";
            case "nlike" :
                return "not like";
            default:
                return " " + termType;
        }
    }
}
